package cn.wolfcode.luowowo.website_api.controller;


import cn.wolfcode.luowowo.QueryObject.StrategyQuery;
import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.StrategyCatalog;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IRegionService;
import cn.wolfcode.luowowo.service.IStrategyCatalogService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.util.GetToast;
import cn.wolfcode.luowowo.util.JsonResult;
import cn.wolfcode.luowowo.util.ParamMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 登录控制器
 */
@RestController
@RequestMapping("/destination")
public class DestinationController {

    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IStrategyService strategyService;

    //查询出热门区域
    @GetMapping("/hotRegion")
    public Object hotRegion() {

        return JsonResult.success(regionService.queryHotRegion());
    }

    //根据区域id 查询目的地
    @GetMapping("/destsByRegionId")
    public Object destsByRegionId(String regionId) {
        List<Destination> list = destinationService.getDescByRegionForApi(regionId);
        return JsonResult.success(new ParamMap()
                .put("list", list)
                .put("regionId", regionId));
    }

    @GetMapping("/detail")
    public Object detail(String id) {
        //toasts 导航吐司
        List<Destination> toasts = destinationService.getToast(id);

        //dest
        Destination dest = toasts.remove(toasts.size() - 1);

        //catalogs
        List<StrategyCatalog> strategyCatalogs = strategyCatalogService.getCatalogByDsetId(id);

        //strategies  查询出前三的攻略文章
        PageRequest of = PageRequest.of(0, 3, Sort.Direction.DESC, "viewnum");
        StrategyQuery qo = new StrategyQuery();
        qo.setPageable(of);
        qo.setDestId(id);
        Page<Strategy> page = strategyService.query(qo);
        //注意 page是对象 包括攻略明细里面所有内容 返回的应该是里面的content文章

        return JsonResult.success(new ParamMap().
                put("toasts", toasts).put("dest", dest)
                .put("catalogs", strategyCatalogs).put("strategies", page.getContent()));
    }

    @GetMapping("/getToast")
    public Object getToast(String id) {
        List<Destination> toasts = GetToast.getToastByDestId(id, destinationService);
        Destination dest = toasts.get(toasts.size() - 1);
        return JsonResult.success(new ParamMap()
                .put("toasts", toasts).put("dest", dest));
    }


}
